# 
# this makefile fragment allows traversal of a design hierachy. it is
# based on work done by Rod Whitby and others at Austek Microsystems
# in 1991.
# 
# $Copyright: (c) 1997-2008 Thomas Dejanovic $ 
#

targets ::
	@echo
	@echo "traversal.mak:"
	@echo "-------------"
	@echo
	@echo "  TARGET=<target> pre-traverse  -- perform <target> on hierarchy"
	@echo "  TARGET=<target> post-traverse -- perform <target> on hierarchy"
	@echo "  TARGET=<target> iterate       -- perform <target> on children"
	@echo
	@echo "  COMMAND=<command> command     -- execute <command> in context"
	@echo
	@echo "  all.<target>   -- make TARGET=<target> post-traverse <target>"
	@echo "  sub.<target>   -- make TARGET=<target> post-traverse"
	@echo

# Default exit commmand (for loop structures)

ifneq (, $(findstring k, $(MAKEFLAGS)))
EXIT_COMMAND = false
else
EXIT_COMMAND = exit 1
endif

.PHONY : pre-traverse post-traverse iterate

pre-traverse :
	@for submodule in ${SUBMODULES} "" ; do \
	  if [ "$$submodule" != "" ] ; then \
	    if [ -d "$$submodule" ] ; then \
	      ( cd $$submodule ; \
		${MAKE} NOT_TOP_OF_ALL=1 ${TARGET} ) || ${EXIT_COMMAND} ; \
	      ( cd $$submodule ; \
		${MAKE} pre-traverse ) || ${EXIT_COMMAND} ; \
	    fi ; \
	  fi ; \
	done

post-traverse :
	@for submodule in ${SUBMODULES} "" ; do \
	  if [ "$$submodule" != "" ] ; then \
	    if [ -d "$$submodule" ] ; then \
	      ( cd $$submodule ; \
		if [ ! -f all.lockout ] ; then \
		  ${MAKE} post-traverse ; \
		fi ; ) || ${EXIT_COMMAND} ; \
	      ( cd $$submodule ; \
		if [ ! -f all.lockout ] ; then \
		  ${MAKE} NOT_TOP_OF_ALL=1 ${TARGET} ; \
		fi ; ) || ${EXIT_COMMAND} ; \
	    fi ; \
	  fi ; \
	done

iterate :
	@for submodule in ${SUBMODULES} "" ; do \
	  if [ "$$submodule" != "" ] ; then \
	    if [ -d "$$submodule" ] ; then \
	      ( cd $$submodule ; \
	        ${MAKE} NOT_TOP_OF_ALL=1 ${TARGET} ) || ${EXIT_COMMAND} ; \
	    fi ; \
	  fi ; \
	done

# Hierarchical pattern rule for normal post-traverse rules

.PHONY : all.%

all.% :
	${MAKE} sub.$*
	${MAKE} TOP_OF_ALL=1 $* 

.PHONY : sub.%

sub.% :
	${MAKE} TARGET="$*" post-traverse


# Rule to execute a specified command (useful when traversing)

command :
	${COMMAND}

#----------------------------------------------------------------------
# Local Variables:
# mode: makefile
# End:
# End of traverse.mak
